package com.coordinate.core.kernel.serviceprovider.impl;

import java.util.List;

/*Email Java Mail API*/
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

import com.coordinate.core.kernel.serviceprovider.EmailServiceProvider;
import com.coordinate.core.message.Message;
import com.coordinate.core.transport.Email;
import com.coordinate.core.type.DeliveryReport;

public class CEmailServiceProvider extends CServiceProvider implements EmailServiceProvider {

	

	public int send(Message pMsg) 
	{
		int lResult = -1;
	
		if(! (pMsg instanceof Email))
		{
			System.out.println("Sorry, this message cannot be sent as an E-mail, throw Exception");
	        return lResult; 		
		}
		
		Email lMail = (Email)pMsg;
		
		/*A temporary E-mail sender*/
		sendEmail(lMail);
		
		return 0;
	}
	
	
	public int  sendEmail(Email pMail)
	{
		int lResult = 0;
		List<String> lList = null;
		Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);
        
        String msgBody = "hello, how are you ?";

        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(pMail.getEmailFrom(), "Admin"));
            
            lList = pMail.getEmailReceipientsList();
            
            for(String lEmailAddress: lList)
            {
              msg.addRecipient(javax.mail.internet.MimeMessage.RecipientType.TO,
                             new InternetAddress(lEmailAddress, "Mr. User"));
            }
            
            /*lList = pMail.getEmailCCList();
            for(String lEmailAddress: lList)
            {
            msg.addRecipient(javax.mail.internet.MimeMessage.RecipientType.CC,
                    new InternetAddress(lEmailAddress, ""));
            }*/
            
            msg.setSubject(pMail.getEmailSubjectLine());
           /* msg.setContent("<html><body bgcolor=\"lightblue\"><h2>This is an e-mail from the Department of Finance</h2>" +
            		"<embed src=http://www.devicemantra.com/coordinate/videos/nike.3gp>" +
            		"<font color=red> Hello again</font>" +
            		
            		"</body></html>",
            		"text/html");*/
            Multipart mp = new MimeMultipart();

            MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(pMail.getEmailBody(), "text/html");
            mp.addBodyPart(htmlPart);
           

            msg.setContent(mp);
            Transport.send(msg);
    
        } catch (AddressException e) {
            e.printStackTrace();
            lResult = -1;
        } catch (MessagingException e) {
        	e.printStackTrace();
        	 lResult = -1;
        }
        catch(Exception e)
        {
        	e.printStackTrace();
        	 lResult = -1;
        }
        
        if(0 == lResult)
        {
        	System.out.println("Sending mail now ! - SUCCESS");
        }
        else
        {
        	System.out.println("Sending mail now ! - FAILURE");
        }
		return lResult;
		
		
	}

	public List<DeliveryReport> getDeliveryReports(String pUniqueMsgId) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
